package base_04_annotation;

import java.lang.annotation.*;

/**
 * 元注解
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/3/25 13:49
 */
@InheritedAnnotation
public class MetaAnnotation {

    private String name;

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        Class<sub> clazz = sub.class;
        Annotation[] annotations = clazz.getAnnotations();
        //runtime类型注解才可被反射获取,且getDeclaredAnnotation忽略继承的注解
        InheritedAnnotation annotation = clazz.getDeclaredAnnotation(InheritedAnnotation.class);
        InheritedAnnotation declaredAnnotation = MetaAnnotation.class.getDeclaredAnnotation(InheritedAnnotation.class);
        System.out.println(annotation);
        System.out.println(declaredAnnotation);
        System.out.println("=============================================");
        for (Annotation anno : annotations) {
            System.out.println(anno.annotationType());
            System.out.println(anno);
            System.out.println("=============================================");
            Class<? extends Annotation> aClass = anno.getClass();
            Class<? extends Annotation> aClazz = anno.annotationType();
            System.out.println(aClass);
            System.out.println(aClazz);
            //获取注解上的注解
            for (Annotation clazzAnnotation : aClazz.getAnnotations()) {
                System.out.println(clazzAnnotation);
            }
        }
    }
}

class sub extends MetaAnnotation {

}

/**
 * @author masque_v
 */
@Documented
@Inherited//可继承
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
@interface InheritedAnnotation {

}
